﻿package morn.core.managers
{
    import __AS3__.vec.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class TimerManager extends Object
    {
        private var _shape:Shape;
        private var _pool:Vector.<TimerHandler>;
        private var _handlers:Dictionary;
        private var _currTimer:int;
        private var _currFrame:int = 0;
        private var _count:int = 0;
        private var _index:uint = 0;

        public function TimerManager()
        {
            this._shape = new Shape();
            this._pool = new Vector.<>;
            this._handlers = new Dictionary();
            this._currTimer = getTimer();
            this._shape.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            return;
        }// end function

        private function onEnterFrame(event:Event) : void
        {
            var _loc_2:Object = null;
            var _loc_3:TimerHandler = null;
            var _loc_4:int = 0;
            var _loc_5:Function = null;
            var _loc_6:Array = null;
            var _loc_7:String = this;
            var _loc_8:* = this._currFrame + 1;
            _loc_7._currFrame = _loc_8;
            this._currTimer = getTimer();
            for (_loc_2 in this._handlers)
            {
                
                _loc_3 = this._handlers[_loc_2];
                _loc_4 = _loc_3.userFrame ? (this._currFrame) : (this._currTimer);
                if (_loc_4 >= _loc_3.exeTime)
                {
                    _loc_5 = _loc_3.method;
                    _loc_6 = _loc_3.args;
                    if (_loc_3.repeat)
                    {
                        while (_loc_4 >= _loc_3.exeTime && _loc_2 in this._handlers)
                        {
                            
                            _loc_3.exeTime = _loc_3.exeTime + _loc_3.delay;
                            _loc_5.apply(null, _loc_6);
                        }
                        continue;
                    }
                    this.clearTimer(_loc_2);
                    _loc_5.apply(null, _loc_6);
                }
            }
            return;
        }// end function

        private function create(param1:Boolean, param2:Boolean, param3:int, param4:Function, param5:Array = null, param6:Boolean = true) : Object
        {
            var _loc_7:Object = null;
            if (param6)
            {
                this.clearTimer(param4);
                _loc_7 = param4;
            }
            else
            {
                var _loc_9:String = this;
                _loc_9._index = this._index + 1;
                _loc_7 = this._index + 1;
            }
            if (param3 < 1)
            {
                param4.apply(null, param5);
                return -1;
            }
            var _loc_8:* = this._pool.length > 0 ? (this._pool.pop()) : (new TimerHandler());
            (this._pool.length > 0 ? (this._pool.pop()) : (new TimerHandler())).userFrame = param1;
            _loc_8.repeat = param2;
            _loc_8.delay = param3;
            _loc_8.method = param4;
            _loc_8.args = param5;
            _loc_8.exeTime = param3 + (param1 ? (this._currFrame) : (this._currTimer));
            this._handlers[_loc_7] = _loc_8;
            var _loc_9:String = this;
            var _loc_10:* = this._count + 1;
            _loc_9._count = _loc_10;
            return _loc_7;
        }// end function

        public function doOnce(param1:int, param2:Function, param3:Array = null, param4:Boolean = true) : Object
        {
            return this.create(false, false, param1, param2, param3, param4);
        }// end function

        public function doLoop(param1:int, param2:Function, param3:Array = null, param4:Boolean = true) : Object
        {
            return this.create(false, true, param1, param2, param3, param4);
        }// end function

        public function doFrameOnce(param1:int, param2:Function, param3:Array = null, param4:Boolean = true) : Object
        {
            return this.create(true, false, param1, param2, param3, param4);
        }// end function

        public function doFrameLoop(param1:int, param2:Function, param3:Array = null, param4:Boolean = true) : Object
        {
            return this.create(true, true, param1, param2, param3, param4);
        }// end function

        public function get count() : int
        {
            return this._count;
        }// end function

        public function clearTimer(param1:Object) : void
        {
            var _loc_2:* = this._handlers[param1];
            if (_loc_2 != null)
            {
                delete this._handlers[param1];
                _loc_2.clear();
                this._pool.push(_loc_2);
                var _loc_3:String = this;
                var _loc_4:* = this._count - 1;
                _loc_3._count = _loc_4;
            }
            return;
        }// end function

    }
}

class TimerHandler extends Object
{
    public var delay:int;
    public var repeat:Boolean;
    public var userFrame:Boolean;
    public var exeTime:int;
    public var method:Function;
    public var args:Array;

    function TimerHandler()
    {
        return;
    }// end function

    public function clear() : void
    {
        this.method = null;
        this.args = null;
        return;
    }// end function

}

